﻿using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using DarkAngels.WoW.Library.Armory.Character;
using Microsoft.WindowsCE.Forms;
using SmartDeviceProject1.Controls;

namespace SmartDeviceProject1
{
    public partial class frmDevelopment : Form
    {
        private PictureButton[] pButtons = null;
        public frmDevelopment()
        {
            this.WindowState = FormWindowState.Maximized;
            this.AutoScaleMode = AutoScaleMode.Dpi;
            m_originalOrientation = SystemSettings.ScreenOrientation;
            InitializeComponent();
            AdjustImageButtons();
            SetVisiblePanel(this.characterListPanel1);
            this.characterListPanel1.CharacterSelectionMethod = new SetSelectedCharacter(this.SetSelectedCharacter);
            SetTestValues();
        }

        private Image GenerateImageWithBackground(Image icon, Image background, int width, int height)
        {
            Bitmap work = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(work);
            int iconWidth = icon.Width;
            int iconHeight = icon.Height;
            int newIconX = ((width - iconWidth) / 2);
            int newIconY = ((height - iconHeight) / 2);
            g.DrawImage(background, 0, 0);
            ImageAttributes attrib = new ImageAttributes();
            Color transparent = GetTransparentColor(icon);
            attrib.SetColorKey(transparent, transparent);
            g.DrawImage(icon, new Rectangle(newIconX, newIconY, icon.Width, icon.Height), 0, 0, icon.Width, icon.Height, GraphicsUnit.Pixel, attrib);
            return work;
        }
        private Image ResizeImage(Image icon, int width, int height)
        {
            //SOURCE: http://www.vbforums.com/showthread.php?p=3466186
            return ResizeImage(icon, width, height, new ImageAttributes());
        }
        private Image ResizeImage(Image icon, int width, int height, ImageAttributes attrib)
        {
            //SOURCE: http://www.vbforums.com/showthread.php?p=3466186
            Bitmap destination = new Bitmap(width, height);
            Rectangle sourceRectangle = new Rectangle(0, 0, icon.Width, icon.Height);
            Rectangle destinationRectangle = new Rectangle(0, 0, width, height);
            Graphics g = Graphics.FromImage(destination);
            g.DrawImage(icon, destinationRectangle, 0, 0, sourceRectangle.Width, sourceRectangle.Height, GraphicsUnit.Pixel, attrib);
            return destination;
        }
        private Color GetTransparentColor(Image image)
        {
            return ((Bitmap)image).GetPixel(0, 0);
        }
        private Image GenerateSimpleBackground(Color color, int width, int height)
        {
            Bitmap bg = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bg);
            SolidBrush sb = new SolidBrush(color);
            g.FillRectangle(sb, 0, 0, width, height);
            return bg;
        }

        private void pictureButton5_Click(object sender, System.EventArgs e)
        {
            Application.Exit();
        }

        private void frmDevelopment_Resize(object sender, System.EventArgs e)
        {
            AdjustImageButtons();
        }

        /// <summary>
        /// This method is called whenever buttons need to be re-adjusted, 
        /// such as when the form has changed size or orientation
        /// </summary>
        private void AdjustImageButtons()
        {
            #region Menu Buttons
            pButtons = new PictureButton[] { pictureButton1, pictureButton2, pictureButton3, pictureButton4 };
            int width = (this.Width + 3 + pButtons.Length); //+3 to remove the borders on each end, +pButtons.Length to make up for overlapping buttons
            Bitmap[] foreground = new Bitmap[] { 
                SmartDeviceProject1.Properties.Resources.blue_user,
                SmartDeviceProject1.Properties.Resources.blue_flag,
                SmartDeviceProject1.Properties.Resources.blue_charts,
                SmartDeviceProject1.Properties.Resources.blue_home
            };
            Bitmap[] background = new Bitmap[] { 
                SmartDeviceProject1.Properties.Resources.yellow_user,
                SmartDeviceProject1.Properties.Resources.yellow_flag,
                SmartDeviceProject1.Properties.Resources.yellow_charts,
                SmartDeviceProject1.Properties.Resources.yellow_home
            };
            for (int i = 0; i < pButtons.Length; i++)
            {
                if (pButtons[i] == null) pButtons[i] = new PictureButton();
                pButtons[i].Text = "";
                pButtons[i].Top = 0;
                pButtons[i].Left = ((width / pButtons.Length) * i) - (i + 1);
                pButtons[i].Height = 50;
                pButtons[i].Width = (width / pButtons.Length);
                pButtons[i].BackgroundImage = GenerateImageWithBackground(
                    ResizeImage(foreground[i], 32, 32),
                    SmartDeviceProject1.Properties.Resources.background_button,
                    pButtons[i].Width,
                    pButtons[i].Height
                );
                pButtons[i].PressedImage = GenerateImageWithBackground(
                    ResizeImage(background[i], 32, 32),
                    SmartDeviceProject1.Properties.Resources.background_button_reverse,
                    pButtons[i].Width,
                    pButtons[i].Height
                );
            }
            #endregion
            #region Close Button
            this.closePictureButton.BackgroundImage = GenerateImageWithBackground(
                ResizeImage(SmartDeviceProject1.Properties.Resources.blue_32x32_door, 16, 16),
                GenerateSimpleBackground(Color.White, 16, 16),
                16,
                16
            );
            this.closePictureButton.PressedImage = GenerateImageWithBackground(
                ResizeImage(SmartDeviceProject1.Properties.Resources.yellow_32x32_door, 16, 16),
                GenerateSimpleBackground(Color.White, 16, 16),
                16,
                16
            );
            this.closePictureButton.Text = "";
            this.closePictureButton.Width = 16;
            this.closePictureButton.Height = 16;
            this.closePictureButton.DrawBorder = false;
            //adjust location on the form, if the size/orientation changes
            this.closePictureButton.Left = this.Width - 16;
            this.closePictureButton.Top = this.Height - 16;
            #endregion
            #region Flip Button
            this.flipPictureButton.BackgroundImage = GenerateImageWithBackground(
                SmartDeviceProject1.Properties.Resources.blue_32x32_Repeat,
                GenerateSimpleBackground(Color.White, 32, 32),
                32,
                32
            );
            this.flipPictureButton.PressedImage = GenerateImageWithBackground(
                SmartDeviceProject1.Properties.Resources.yellow_32x32_Repeat,
                GenerateSimpleBackground(Color.White, 32, 32),
                32,
                32
            );
            this.flipPictureButton.Text = "";
            this.flipPictureButton.DrawBorder = false;
            #endregion
            #region Character Panels
            this.characterPanel1.Top = 50;
            this.characterPanel1.Left = 0;
            this.characterPanel1.Height = this.Height - 50;
            this.characterPanel1.Width = this.Width;
            #endregion
            #region Achievement Panels
            this.achievementPanel1.Top = 50;
            this.achievementPanel1.Left = 0;
            this.achievementPanel1.Height = this.ClientSize.Height - 50;
            this.achievementPanel1.Width = this.ClientSize.Width;
            #endregion
            #region Statistics Panels
            this.statisticsPanel1.Top = 50;
            this.statisticsPanel1.Left = 0;
            this.statisticsPanel1.Height = this.Height - 50;
            this.statisticsPanel1.Width = this.Width;
            #endregion
            #region Character List Panels
            this.characterListPanel1.Top = 50;
            this.characterListPanel1.Left = 0;
            this.characterListPanel1.Height = this.Height - 50;
            this.characterListPanel1.Width = this.Width;
            #endregion
        }

        private void SetOrientation(ScreenOrientation so)
        {
            SystemSettings.ScreenOrientation = so;
        }
        int m_screenOrientation = 0;
        private ScreenOrientation m_originalOrientation = ScreenOrientation.Angle0;
        private void flipPictureButton_Click(object sender, System.EventArgs e)
        {
            //flip screen
            switch (m_screenOrientation)
            {
                case 0:
                    SetOrientation(ScreenOrientation.Angle90);
                    m_screenOrientation++;
                    break;
                case 1:
                    SetOrientation(ScreenOrientation.Angle180);
                    m_screenOrientation++;
                    break;
                case 2:
                    SetOrientation(ScreenOrientation.Angle270);
                    m_screenOrientation++;
                    break;
                default:
                    SetOrientation(ScreenOrientation.Angle0);
                    m_screenOrientation = 0;
                    break;
            }
        }
        private void frmDevelopment_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //reset phone to default orientation
            SetOrientation(m_originalOrientation);
            characterListPanel1.SaveXml();
        }
        private void pictureButton1_Click(object sender, System.EventArgs e)
        {
            if (sender.Equals(this.pictureButton1))
            {
                SetVisiblePanel(this.characterPanel1);
            }
            else if (sender.Equals(this.pictureButton2))
            {
                SetVisiblePanel(this.achievementPanel1);
            }
            else if (sender.Equals(this.pictureButton3))
            {
                SetVisiblePanel(this.statisticsPanel1);
            }
            else if (sender.Equals(this.pictureButton4))
            {
                SetVisiblePanel(this.characterListPanel1);
            }
        }
        object[] panels = null;
        private void SetVisiblePanel(object visible) {
            panels = new object[] { this.characterPanel1, this.statisticsPanel1, this.characterListPanel1, this.achievementPanel1 };
            for (int i = 0; i < panels.Length; i++)
            {
                if (panels[i].Equals(visible))
                    (panels[i] as UserControl).Visible = true;
                else
                    (panels[i] as UserControl).Visible = false;
            }
        }
        public void SetSelectedCharacter(Character c)
        {
            this.characterPanel1.SetCharacter(c);
            this.statisticsPanel1.SetCharacter(c);
            this.achievementPanel1.SetCharacter(c);
        }

        private void SetTestValues()
        {
            List<Character> characters = new List<Character>();
            Character c1 = new Character("nemu", "Twilight's Hammer", DarkAngels.WoW.Library.Armory.ArmoryRegion.EU);
            Character c2 = new Character("kayama", "Twilight's Hammer", DarkAngels.WoW.Library.Armory.ArmoryRegion.EU);
            Character c4 = new Character("blinked", "Twilight's Hammer", DarkAngels.WoW.Library.Armory.ArmoryRegion.EU);
            Character c5 = new Character("ironheal", "Twilight's Hammer", DarkAngels.WoW.Library.Armory.ArmoryRegion.EU);
            characters.Add(c1);
            characters.Add(c2);
            characters.Add(c4);
            characters.Add(c5);
            foreach (Character c in characters)
            {
                c.Load();
            }
            this.characterListPanel1.Characters = characters;
        }
    }
}